<div id="tabs-1"
	style=" padding: 10px; width: 45%">
	<h3><span id="obj-tab">Objects</span> >> Manage Objects</h3>
	
	<hr>
	<div id="objcontent">&nbsp;</div>
	<div id="obj_input">
		<input type="text" name="obj_in" id="obj_in"
			style="width: 500px; padding: 5px; margin-right: 10px;" />
		<button id="obj_sub" value="Add"
			style="padding: 5px; margin-right: 10px;">Add Object</button>
		<button id="obj_sub_update" value="Update"
			style="padding: 5px; margin-right: 10px;">Update Object</button>
	</div>

</div>

<script text="text/javascript">


//objegories
function loadObjects(){
	$.getJSON('<?php print base_url(); ?>xmlrpc_client/get_all_objects',function(data){
		console.log(data);
		if(data.length>0){
	    var text='<div class="tobjects">';
	    var i=1;
		$.each(data, function(key, val) {
		      text=text+'<ul objid="'+val.id+'" class="table objdiv ui-widget ui-helper-clearfix">'
		      +'<li class="ui-state-default ui-corner-all editobj" ><span class="ui-icon ui-icon-pencil"></span></li>'
		      +'<li class="ui-state-default ui-corner-all delobj" ><span class="ui-icon ui-icon-trash"></span></li>'
		      +'<li class="namebox">'+val.name+'</li></ul>';
		      
		      i++;
		});	
		text=text+'</div>';
		}else{
			text="No Objects available, Please add them using the form below.";
		}
		$('#objcontent').html(text);	
		$('#obj_sub_update').hide();
		$('#obj_sub').show();
		$('#obj_in').val("")
		associateObjectActions();

		
	});
	
}

function associateObjectActions(){

	console.log('associalting actions');
	$('.editobj').click(function(){
			inobj=$(this);
			ulobj=inobj.parent();
			nameobj=ulobj.children(".namebox");
			$('#obj_in').val(nameobj.text());
			$('#obj_sub_update').attr('objid',ulobj.attr('objid'));
			$('#obj_sub').hide();
			$('#obj_sub_update').show();
	});
	
	$('.delobj').click(function(){
			inobj=$(this);
			ulobj=inobj.parent();
			objid=ulobj.attr('objid');
			//Post and delete
			var dataString='&objid='+objid;
			$.ajax({
			type: "POST",
				url: "<?php print base_url(); ?>xmlrpc_client/delete_object",
				data: dataString,
				success : function(data){
					$.jGrowl("Object deleted.!");
					loadObjects();
				}
			});			
	});


	
}


$(document).ready(function(){

   //load objects
   	loadObjects();

	$('#obj-tab').click(function(){
		$('#tab-container').load("<?php print base_url(); ?>admin/loadtab/mngobjs");
	});
	
   //load fields
	$('#obj_sub').button().click(function(){
		var text=$('#obj_in').val();
		var objid=$(this).attr('objid');
		console.log('clicked');
		if(text != "" ){
			var dataString='&objid='+objid+'&name='+text;
				$.ajax({
				type: "POST",
					url: "<?php print base_url(); ?>xmlrpc_client/create_object",
					data: dataString,
					success : function(data){
						$.jGrowl("object created.!");
						loadObjects();
					}
				});
		}
	});

	$('#obj_sub_update').button().click(function(){
		var text=$('#obj_in').val();
		var objid=$(this).attr('objid');
		if(text != "" ){
			var dataString='&objid='+objid+'&name='+text;
				$.ajax({
				type: "POST",
					url: "<?php print base_url(); ?>xmlrpc_client/update_object",
					data: dataString,
					success : function(data){
						$.jGrowl("objegory updated.!");
						loadObjects();
					}
				});
		}
	});
	
});
</script>
